<?php
/**
 * Created by PhpStorm.
 * User: tinker
 * Date: 2021/2/9
 * Time: 18:39
 */
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\PasswordLists;
use Illuminate\Support\Facades\Request;

class HomeController extends Controller
{
    /**
     * 显示指定用户的简介
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        return view('home.index', ['lists' => PasswordLists::all()]);
    }

    public function getPwd(){
        //$length =mt_rand(12,20);
        $length =11;
        $pwd_str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $special_str = '#$%@*-_';
        $shuffle_str = str_shuffle($pwd_str);
        $number = $length > strlen($shuffle_str) - 1 ? strlen($shuffle_str) - 1:$length;
        $pwd = substr($shuffle_str,0,$number).$special_str[mt_rand(0,6)];
        $pwd_model = new PasswordLists;
        $pwd_model::where(['status'=>1])->update(['status' => 0]);
        $pwd_model->passwords = $pwd;
        $pwd_model->description = "随机生成的密码";
        $pwd_model->status = 1;
        $pwd_model->save();
        return redirect('/')->with('msg', '创建成功，新密码是：'.$pwd);
    }

    public function ruleGetPwd(Request $request){
        $enWord = $request::post("EnWord");
        $bigEnWord = $request::post("BigEnWord");
        $num = $request::post("Num");
        $specail = $request::post("Specail");
        $pwd_rule = [
            'small_alpha' =>($enWord-$bigEnWord),
            'big_alpha' =>$bigEnWord,
            'number' =>$num,
            'sign' =>$specail,
        ];

        $pwd = $this->random_code_type(12,$pwd_rule);
        $pwd_model = new PasswordLists;
        $pwd_model::where(['status'=>1])->update(['status' => 0]);
        $pwd_model->passwords = $pwd;
        $pwd_model->description = "随机生成的密码";
        $pwd_model->status = 1;
        $pwd_model->save();
        return redirect('/')->with('msg', '创建成功，新密码是：'.$pwd);
    }
    public function setUse($id){

        $pwd_model = new PasswordLists;
        $pwd_model::where(['status'=>1])->update(['status' => 0]);
        $pwd_model::where(['id'=>$id])->update(['status' => 1]);

        return redirect('/')->with('msg', '操作成功');
    }

    public function unsetUse($id){

        $pwd_model = new PasswordLists;
        $pwd_model::where(['status'=>1])->update(['status' => 0]);
        $pwd_model::where(['id'=>$id])->update(['status' =>0]);

        return redirect('/')->with('msg', '操作成功');
    }
    public function delete($id){

        $pwd_model = new PasswordLists;
        $pwd_model::where(['id'=>$id])->delete();

        return redirect('/')->with('msg', '操作成功');
    }

    private function random_code_type($length = 12,$rule =[]){
        if(empty($rule)){
            return [];
        }

        $code_arr = array(
            'small_alpha' => 'abcdefghijklmnopqrstuvwxyz',
            'big_alpha' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'number'=> '0123456789',
            'sign'  => '#$%@*-_',
        );
        $pwd_str = '';
        $strlength = 0;
        foreach ($rule as $str_style => $num){
            $strlength += $num;
            $str = substr(str_shuffle($code_arr[$str_style]),0,$num);
            $pwd_str.=$str;
        }
        if($strlength != $length){
            return [];
        }
        return $pwd_str;
    }
}
